package co.chuangdian.oflauncher.folders;

import java.io.File;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.annotation.SuppressLint;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;
import cn.onekit.ASSET;
import cn.onekit.CALLBACK;
import cn.onekit.CONFIG;
import cn.onekit.FSO;
import cn.onekit.JSON;
import cn.onekit.MESSAGE;
import cn.onekit.android.ACTIVITY;
import co.chuangdian.oflauncher.R;
import co.chuangdian.oflauncher.folders.setting.HomeActivity;
import uc.FolderviewType;

public class FoldersFragment extends Fragment {

	View headerView;

	ListView foldersListView;
	ListView foldersListViewSimple;

	ViewGroup row = null;
	ViewHolder mHolder;

	Context context = null;
	int iconResID;
	int icon;

	JSONObject folder_style;
	int folder_style_int;

	private SwipeRefreshLayout mRefreshfolderView;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.folders, container, false);

		// 5-9

		foldersListView = (ListView) view.findViewById(R.id.foldersListView);

		mRefreshfolderView = (SwipeRefreshLayout) view.findViewById(R.id.refresh_folder);
		mRefreshfolderView.setOnRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh() {
				// TODO Auto-generated method stub
				mHandler.sendEmptyMessage(0);
			}
		});

		foldersListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
				Bundle bundle = new Bundle();
				bundle.putInt("index", position);
				MESSAGE.send("changeFolder", null, bundle);
			}
		});
		// 第二个
		foldersListViewSimple = (ListView) view.findViewById(R.id.foldersListViewSimple);

		foldersListViewSimple.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				if (arg2 != 0) {
					arg2 = arg2 - 1;
					Bundle bundle = new Bundle();
					bundle.putInt("index", arg2);
					MESSAGE.send("changeFolder", null, bundle);
				} else {
					arg0.setClickable(false);
				}
			}
		});

		return view;
	}

	JSONArray folders;

	@SuppressLint("InflateParams")
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		loadSetting();
		showListViewType();

		// 5-10
		MESSAGE.receive("change_folder_view", null, new CALLBACK<Bundle>() {

			@Override
			public void run(boolean isError, Bundle result) {
				loadSetting();
				showListViewType();
				// loadSetting();
				// Toast.makeText(getActivity(), folder_style_int, 1000).show();
			}
		});

		folders = JSON.parses((String) CONFIG.get("folders"));
		foldersListView.setAdapter(new BaseAdapter() {

			@Override
			public int getCount() {
				return folders.length();
			}

			@Override
			public Object getItem(int position) {
				try {
					return folders.getJSONObject(position);
				} catch (Exception e) {
					e.printStackTrace();
					return null;
				}
			}

			@Override
			public long getItemId(int position) {
				return position;
			}

			@SuppressLint("ViewHolder")
			@Override
			public View getView(int position, View convertView, ViewGroup parent) {

				JSONObject folder = folders.optJSONObject(position);
				row = (ViewGroup) View.inflate(getActivity(), R.layout.folders_row, null);

				if (position == 0) {
					row.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT, ACTIVITY.dp2px(250)));
				} else {
					row.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT, ACTIVITY.dp2px(80)));
				}
				mHolder = new ViewHolder();
				// 把布局文件中所有组件的对象封装至ViewHolder对象中
				mHolder.name = (TextView) row.findViewById(R.id.iconText);
				mHolder.icon = (ImageView) row.findViewById(R.id.iconImageView);
				mHolder.cover = (ImageView) row.findViewById(R.id.coverImageView);
				// 把ViewHolder对象封装至View对象中
				row.setTag(mHolder);
				int resID = getResources().getIdentifier(folder.optString("icon"), "drawable",
						getActivity().getPackageName());
				File path = FSO.photos(folder.optString("cover"));
				Bitmap cover = FSO.loadImage(path.getPath(), false);
				try {
					mHolder.cover.setImageBitmap(cover);
					mHolder.name.setText(folder.getString("name"));
					mHolder.icon.setImageResource(resID);
				} catch (JSONException e) {
					e.printStackTrace();
				}

				return row;
			}
		});

		// 第二个
		LayoutInflater lif = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		headerView = lif.inflate(R.layout.folders_row_mini, null);
		JSONObject folder = folders.optJSONObject(0);
		File path = FSO.photos(folder.optString("cover"));
		Bitmap cover = FSO.loadImage(path.getPath(), false);
		BitmapDrawable bd = new BitmapDrawable(cover);
		headerView.setBackground(bd);

		// Bitmap bp = ASSET.loadImage("folders/6.jpg", false);
		// @SuppressWarnings("deprecation")
		// BitmapDrawable bd=new BitmapDrawable(bp);
		// headerView.setBackground(bd);

		// headerView.setBackground(background);
		foldersListViewSimple.addHeaderView(headerView);
		foldersListViewSimple.setAdapter(new BaseAdapter() {

			@Override
			public int getCount() {
				return folders.length();
			}

			@Override
			public Object getItem(int position) {
				try {
					return folders.getJSONObject(position);
				} catch (Exception e) {
					e.printStackTrace();
					return null;
				}
			}

			@Override
			public long getItemId(int position) {
				return position;
			}

			@SuppressWarnings("deprecation")
			@SuppressLint("ViewHolder")
			@Override
			public View getView(int position, View convertView, ViewGroup parent) {

				JSONObject folder = folders.optJSONObject(position);
				row = (ViewGroup) View.inflate(getActivity(), R.layout.folders_row_simple, null);
				row.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT, ACTIVITY.dp2px(70)));
				try {
					mHolder = new ViewHolder();
					// 把布局文件中所有组件的对象封装至ViewHolder对象中
					mHolder.name = (TextView) row.findViewById(R.id.iconText);
					mHolder.icon = (ImageView) row.findViewById(R.id.iconImageView);
					mHolder.cover = (ImageView) row.findViewById(R.id.coverImageView);
					// 把ViewHolder对象封装至View对象中
					row.setTag(mHolder);
					int resID = getResources().getIdentifier(folder.optString("icon"), "drawable",
							getActivity().getPackageName());
					// mHolder.cover.setAlpha(100);
					mHolder.name.setText(folder.getString("name"));
					mHolder.icon.setImageResource(resID);
				} catch (JSONException e) {
					e.printStackTrace();
				}
				return row;
			}
		});
	}

	void ListViewVisiable() {
		if (folder_style.equals(0)) {
			foldersListView.setVisibility(View.VISIBLE);
			foldersListViewSimple.setVisibility(View.GONE);
		} else {
			foldersListView.setVisibility(View.GONE);
			foldersListViewSimple.setVisibility(View.VISIBLE);
		}
	}

	void loadSetting() {
		folder_style = JSON.parse((String) CONFIG.get("folders_setting"));
		// folder_style.optInt("view")
		// folder_style_int = folder_style.optInt("view");
	}

	class ViewHolder {
		TextView name;
		ImageView icon;
		ImageView cover;
	}

	void showListViewType() {
		int folder_style_int = folder_style.optInt("view");
		FolderviewType folderview_type = FolderviewType.values()[folder_style_int];
		switch (folderview_type) {
		case normal:
			foldersListView.setVisibility(View.VISIBLE);
			foldersListViewSimple.setVisibility(View.GONE);
			break;
		case simple:
			foldersListView.setVisibility(View.GONE);
			foldersListViewSimple.setVisibility(View.VISIBLE);
			break;
		}
	}

	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			startActivity(new Intent(getActivity(), HomeActivity.class));
			getActivity().overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
			mRefreshfolderView.setRefreshing(false);
		};
	};

}
